﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace Project_ScansiaPacific
{
    public partial class FrNhaCungCap : DevComponents.DotNetBar.Office2007Form
    {
        public FrNhaCungCap()
        {
            InitializeComponent();
        }
        private string MaNCC1,TenNCC1,DiaChi1,DienThoai1,NguoiLH1,Email1,DTDD1,SoFax1,SoTK1,MSThue1,TenNH1,child=" - ";
        public string sql = @"select * from dbo.NhaCungCap";
        public void gridview_upload()
        {
            Data dta = new Data();
            DataSet ds = dta.dts(sql);
            dataGridView1.DataSource = ds.Tables[0];
            dataGridView1.ReadOnly = true;
        }
        public void Display(int i)
        {
            try
            {
                txt_MaNCC.Text = dataGridView1.Rows[i].Cells["MaNCC"].Value.ToString();
                txt_TenNCC.Text = dataGridView1.Rows[i].Cells["TenNCC"].Value.ToString();
                txt_DiaChi.Text = dataGridView1.Rows[i].Cells["DiaChi"].Value.ToString();
                txt_DienThoai.Text = dataGridView1.Rows[i].Cells["DienThoai"].Value.ToString();
                txt_NguoiLH.Text = dataGridView1.Rows[i].Cells["NguoiLH"].Value.ToString();
                txt_DTDD.Text = dataGridView1.Rows[i].Cells["DTDD"].Value.ToString();
                txt_SoFax.Text = dataGridView1.Rows[i].Cells["SoFax"].Value.ToString();
                txt_SoTK.Text = dataGridView1.Rows[i].Cells["SoTK"].Value.ToString();
                cb_TenNH.Text = dataGridView1.Rows[i].Cells["TenNH"].Value.ToString();
                txt_MSThue.Text = dataGridView1.Rows[i].Cells["MSThue"].Value.ToString();
                dateTimePicker1.Text = dataGridView1.Rows[i].Cells["Ngay"].Value.ToString();
                lb_gridcount.Text = "Có " + (dataGridView1.Rows.Count - 1).ToString() + " Nhà CC";
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        private void combo_load()
        {
            FormProcess Frp = new FormProcess();
            string[] arr = new string[] { "BIDV", "VietComBank", "VietinBank", "HDBank", "DaiABank", "SaComBank", "DongABank", "AnBinhBank", "EximBank", "AGriBank", "KienLongBank", "MaritimesBank", "VietABank", "NamABank", "ACB", "MHB", "PGBank", "PhuongNamBank", "MB", "TrustBank", "VIB", "VPBank" };
            for (int i = 0; i < arr.Length; i++)
            {
                cb_TenNH.Items.Add(arr[i].ToString());
            }
            string select_sql = @"select MaNCC,TenNCC from dbo.NhaCungCap";
            Frp.cb1(cb_search, select_sql, 0, 1);
        }

        private void FrNhaCungCap_Load(object sender, EventArgs e)
        {
            Data dta = new Data();
            txt_MaNCC.ReadOnly = true;
            dta.datimes(lb_date);
            gridview_upload();
            combo_load();
            Display(0);
        }
        public void Save()
        {
            Data dta = new Data();
            MaNCC1 = txt_MaNCC.Text.Trim();
            TenNCC1 = txt_TenNCC.Text.Trim();
            DiaChi1 = txt_DiaChi.Text.Trim();
            DienThoai1 = txt_DienThoai.Text.Trim();
            NguoiLH1 = txt_NguoiLH.Text.Trim();
            Email1 = txt_Email.Text.Trim();
            DTDD1 = txt_DTDD.Text.Trim();
            SoFax1 = txt_SoFax.Text.Trim();
            SoTK1 = txt_SoTK.Text.Trim();
            TenNH1 = cb_TenNH.Text;
            MSThue1 = txt_MSThue.Text.Trim();
            if (txt_MaNCC.Text == "" || txt_TenNCC.Text == "")
                MessageBox.Show("Bạn vui lòng nhập Mã NCC và Tên NCC");
            else
            {
                try
                {
                    SqlConnection Conn = dta.Connect();
                    Conn.Open();
                    string sql_insert = @"insert into dbo.NhaCungCap(MaNCC,TenNCC,DiaChi,DienThoai,NguoiLH,Email,DTDD,SoFax,SoTK,TenNH,MSThue) values(N'";
                    sql_insert += MaNCC1 + "',N'" + TenNCC1 + "',N'" + DiaChi1 + "',N'" + DienThoai1 + "',N'" + NguoiLH1 + "',N'" + Email1 + "',N'" + DTDD1 + "',N'" + SoFax1 + "',N'";
                    sql_insert += SoTK1 + "',N'" + TenNH1 + "',N'" + MSThue1 + "')";
                    SqlCommand cmd = new SqlCommand(sql_insert, Conn);
                    cmd.ExecuteNonQuery();
                    MessageBox.Show("Dữ liệu đã được thêm thành công");
                    dta.reset_txt(groupBox1);
                    bt_save.Enabled = false;
                    //MessageBox.Show(sql_insert);
                    txt_MaNCC.ReadOnly = true;
                    Conn.Close();
                    cmd.Dispose();
                }
                
                catch(Exception ex)
                {
                    string s = "insert duplicate key";
                    string FK = "FOREIGN KEY constraint";
                    string s1 = ex.Message.ToString();
                    if (s1.Contains(s))
                    { MessageBox.Show("Insert không thành công \n" + "Mã đã tồn tại "); }
                    else if (s1.Contains(FK))
                    { MessageBox.Show("Nhập sai thông tin trong Combobox"); }
                    else
                        MessageBox.Show(ex.Message);
                }
            }
            //else
                //dataGridView1.Rows.Add(MaNCC1, TenNCC1, DiaChi1, DienThoai1, NguoiLH1, Email1, DTDD1, SoFax1,SoTK1,TenNH1, MSThue1);
            //Data dta = new Data();
           //if (txt_MaCty.Text != "")
                /*try
                {
                    for (int i = dataGridView1.Rows.Count - 2; i >= 0; i--)
                    {
                        MaNCC1 = dataGridView1.Rows[i].Cells["MaNCC"].Value.ToString();
                        TenNCC1 = dataGridView1.Rows[i].Cells["TenNCC"].Value.ToString();
                        DiaChi1 = dataGridView1.Rows[i].Cells["DiaChi"].Value.ToString();
                        DienThoai1 = dataGridView1.Rows[i].Cells["DienThoai"].Value.ToString();
                        NguoiLH1 = dataGridView1.Rows[i].Cells["NguoiLH"].Value.ToString();
                        Email1 = dataGridView1.Rows[i].Cells["Email"].Value.ToString();
                        DTDD1 = dataGridView1.Rows[i].Cells["DTDD"].Value.ToString();
                        SoFax1 = dataGridView1.Rows[i].Cells["SoFax"].Value.ToString();
                        SoTK1 = dataGridView1.Rows[i].Cells["SoTK"].Value.ToString();
                        MSThue1 = dataGridView1.Rows[i].Cells["MSThue"].Value.ToString();
                        Ngay1 = dataGridView1.Rows[i].Cells["Ngay"].Value.ToString();
                    }                
                }
                catch (Exception ex)
                { MessageBox.Show(ex.Message); }*/
        }

        private void bt_insert_Click_1(object sender, EventArgs e)
        {
            txt_MaNCC.ReadOnly = false;
            Data dta = new Data();
            dta.reset_txt(groupBox1);
            txt_MaNCC.Focus();
            bt_save.Enabled = true;
            bt_edit.Enabled = false;
            bt_delete.Enabled = false;
        }

        private void bt_save_Click_1(object sender, EventArgs e)
        {
            Save();
            bt_delete.Enabled = true;
            bt_edit.Enabled = true;
     
        }

        private void bt_delete_Click_1(object sender, EventArgs e)
        {
            Data dta=new Data();
            SqlConnection Conn = dta.Connect();
            try
            {
                Conn.Open();
                if (txt_MaNCC.Text != "")
                {
                    bt_save.Enabled = false;
                    // Data dta = new Data();
                    string delete = @"delete from dbo.NhaCungCap where MaNCC = N'" + txt_MaNCC.Text + "'";
                    DialogResult d = MessageBox.Show("Bạn có chắc muốn xóa khách hàng " + txt_MaNCC.Text, "Thông Báo", MessageBoxButtons.OKCancel);
                    if (d == DialogResult.OK)
                    {
                        SqlCommand cmd = new SqlCommand(delete, Conn);
                        cmd.ExecuteNonQuery();
                        MessageBox.Show(" Đã xóa thành công");
                        dta.reset_txt(groupBox1);
                        cmd.Dispose();
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Đã có lỗi xảy ra khi xóa dữ liệu, vui lòng kiểm tra lại", ex.Message);
            }
            finally
            {
                Conn.Close();
            }
        }

        private void bt_edit_Click_1(object sender, EventArgs e)
        {
            if (txt_MaNCC.Text != "")
            {
                Data dta = new Data();
                DialogResult d = MessageBox.Show("Bạn có chắc muốn sửa Thông Tin của NCC " + txt_MaNCC.Text, "Thông Báo", MessageBoxButtons.OKCancel);
                if (d == DialogResult.OK)
                {
                    try
                    {
                        SqlConnection Conn = dta.Connect();
                        Conn.Open();
                        string sql_update = null;
                        sql_update = "update dbo.NhaCungCap set TenNCC=N'" + txt_TenNCC.Text.Trim() + "',DiaChi=N'" + txt_DiaChi.Text.Trim() + "',DienThoai=N'";
                        sql_update += txt_DienThoai.Text.Trim() + "',NguoiLH=N'" + txt_NguoiLH.Text.Trim() + "',Email=N'" + txt_Email.Text.Trim() + "',DTDD=N'";
                        sql_update += txt_DTDD.Text.Trim() + "',SoFax=N'" + txt_SoFax.Text + "',SoTK=N'" + txt_SoTK.Text.Trim() + "',TenNH=N'" + cb_TenNH.Text.Trim() + "',MSThue=N'";
                        sql_update += txt_MSThue.Text.Trim() + "' where MaNCC=N'" + txt_MaNCC.Text.Trim() + "'";
                        //MessageBox.Show(sql_update);
                        SqlCommand cmd = new SqlCommand(sql_update, Conn);
                        cmd.ExecuteNonQuery();
                        MessageBox.Show("Thông tin chỉnh sửa thành công");
                        dta.reset_txt(groupBox1);
                        Conn.Close();
                        cmd.Dispose();
                    }
                    catch
                    {
                        MessageBox.Show("Đã có lỗi xảy ra khi chỉnh sửa dữ liệu, Vui lòng kiểm tra lại");
                    }
                    bt_save.Enabled = false;
                    txt_MaNCC.ReadOnly = true;
                }
            }
        }

        private void dataGridView1_Click_1(object sender, EventArgs e)
        {
            int i = dataGridView1.CurrentCell.RowIndex;
            Display(i);
            txt_MaNCC.ReadOnly = true;
        }

        private void bt_first_Click(object sender, EventArgs e)
        {
            bt_save.Enabled = false;
            if (dataGridView1.Rows.Count > 0)
            {
                lb_index.Text = "0";
                Display(0);
            }
        }

        private void bt_prev_Click(object sender, EventArgs e)
        {
            bt_save.Enabled = false;
            if (dataGridView1.Rows.Count > 0)
            {
                int i;
                i = int.Parse(lb_index.Text);
                if (i > 0)
                {
                    i--;
                    Display(i);
                    lb_index.Text = i.ToString();
                }
            }
        }

        private void bt_next_Click(object sender, EventArgs e)
        {
            bt_save.Enabled = false;
            try
            {
                if (dataGridView1.Rows.Count > 0)
                {
                    int i;
                    i = int.Parse(lb_index.Text);
                    if (i < dataGridView1.Rows.Count - 2)
                    {
                        i++;
                        Display(i);
                        lb_index.Text = i.ToString();
                    }
                }
            }
            catch (Exception ex)
            { MessageBox.Show(ex.Message); }
        }

        private void bt_last_Click(object sender, EventArgs e)
        {
            bt_save.Enabled = false;
            if (dataGridView1.Rows.Count > 0)
            {
                lb_index.Text = (dataGridView1.Rows.Count - 2).ToString();
                int i = int.Parse(lb_index.Text);
                Display(i);
            }
        }
        private void search()
        {
            Process prs = new Process();
            string sql_select = null;
            string s = null;
            lb_index.Text = "0";
            if (cb_search.Text == "")
                MessageBox.Show("Bạn vui lòng nhập thông tin cần tìm");

            else
            {
                try
                {
                    if (rd_MaNCC.Checked)
                    {
                        s = prs.substring_left(cb_search.Text, child);
                        if (s == "")
                            s = cb_search.Text;
                        sql_select = sql + " where MaNCC like N'" + s + "'";
                    }
                    if (rd_TenNCC.Checked)
                    {
                        s = prs.substring_right(cb_search.Text, child);
                        if (s == "")
                            s = cb_search.Text;
                        sql_select = sql + " where TenNCC like N'" + s + "'";
                    }
                    DataSet ds = prs.dts(sql_select);
                    if (ds.Tables[0].Rows.Count > 0)
                    {
                        dataGridView1.DataSource = ds.Tables[0];
                        Display(0);
                    }
                    else
                        MessageBox.Show("Không tìm thấy kết quả nào");
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
        }

        private void bt_search_Click(object sender, EventArgs e)
        {
            search();
        }

        private void FrNhaCungCap_KeyDown(object sender, KeyEventArgs ex)
        {
            if (ex.KeyCode == Keys.F5)
                gridview_upload();
            if (ex.KeyData == Keys.Enter)
            {
                SendKeys.Send("{TAB}");
            }
            if (ex.KeyCode == Keys.F1)
                bt_insert_Click_1(null, null);
            if (ex.KeyCode == Keys.F11)
                Save();

        }



        
    }
}
